package com.ml.controller;

import com.ml.model.AjaxMsg;
import com.ml.model.KqData;
import com.ml.model.MLlog;
import com.ml.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping(value = "kq")
public class KqController {
    @Autowired
    private SystemService systemService;
    @Autowired
    private MLUserService mLUserService;
    @Autowired
    private KqAccessService kqAccessService;
    @Autowired
    private KqDataService kqDataService;
    @Autowired
    private MLlogService mLlogService;
    /**
     * 返回一个json对象
     */
    @RequestMapping({"/test"})
    @ResponseBody
    public AjaxMsg getById(HttpServletRequest request) {
        return AjaxMsg.returnAjaxMsg();
    }

    /**
     * 同步 指定天的 考勤数据 上海
     */
    @RequestMapping({"/sh/date"})
    @ResponseBody
    public AjaxMsg writeDate(HttpServletRequest request, HttpServletResponse response) {
        String year = request.getParameter("year");
        String month  = request.getParameter("month");
        String day  = request.getParameter("day");
        String date  = year + "/" + month + "/" + day;
        int i = 0;
        String msg = "";
        try{
            i = kqAccessService.writeMysqlDkInfoSh(date);
           msg = "同步上海考勤数据成功，成功同步 "+i+" 条 "+date+" 的考勤数据！";
            mLlogService.save(new MLlog(0,"同步考勤",msg,request));//保存个日志
        }catch (Exception e){
            msg = "同步上海"+date+"考勤数据失败。";
            mLlogService.save(new MLlog(1,"同步考勤",msg,request));//保存个日志
        }
        return AjaxMsg.returnAjaxMsg(i,msg);
    }

    /**
     * 同步 指定天的 考勤数据 天津
     */
    @RequestMapping({"/tj/date"})
    @ResponseBody
    public AjaxMsg writeDateTj(HttpServletRequest request, HttpServletResponse response) {
        String year = request.getParameter("year");
        String month  = request.getParameter("month");
        String day  = request.getParameter("day");
        String date  = year + "/" + month + "/" + day;
        int i = 0;
        String msg = "";
        try{
            i = kqAccessService.writeMysqlDkInfoTj(date);
            msg = "同步天津考勤数据成功，成功同步 "+i+" 条 "+date+" 的考勤数据！";
            mLlogService.save(new MLlog(0,"同步考勤",msg,request));//保存个日志
        }catch (Exception e){
            msg = "同步天津"+date+"考勤数据失败。";
            mLlogService.save(new MLlog(1,"同步考勤",msg,request));//保存个日志
        }
        return AjaxMsg.returnAjaxMsg(i,msg);
    }

    /**
     * 同步 指定天的 考勤数据 大连
     */
    @RequestMapping({"/dl/date"})
    @ResponseBody
    public AjaxMsg writeDateDl(HttpServletRequest request, HttpServletResponse response) {
        String year = request.getParameter("year");
        String month  = request.getParameter("month");
        String day  = request.getParameter("day");
        String date  = year + "/" + month + "/" + day;
        int i = 0;
        String msg = "";
        try{
            i = kqAccessService.writeMysqlDkInfoDl(date);
            msg = "同步大连考勤数据成功，成功同步 "+i+" 条 "+date+" 的考勤数据！";
            mLlogService.save(new MLlog(0,"同步考勤",msg,request));//保存个日志
        }catch (Exception e){
            msg = "同步大连"+date+"考勤数据失败。";
            mLlogService.save(new MLlog(1,"同步考勤",msg,request));//保存个日志
        }
        return AjaxMsg.returnAjaxMsg(i,msg);
    }


    /**
     * 同步 指定天的 考勤数据 大连、上海、天津一起同步
     */
    @RequestMapping({"/all/date"})
    @ResponseBody
    public AjaxMsg writeDateAll(HttpServletRequest request, HttpServletResponse response) {
        String year = request.getParameter("year");
        String month  = request.getParameter("month");
        String day  = request.getParameter("day");
        String date  = year + "/" + month + "/" + day;
        int n = 0;
        String msg = "";
        try{
            int i = kqAccessService.writeMysqlDkInfoSh(date);
            int j = kqAccessService.writeMysqlDkInfoTj(date);
            int k = kqAccessService.writeMysqlDkInfoDl(date);
            n = (i+j+k);
            msg = "同步考勤数据成功，成功同步 "+n+" 条 "+date+" 的考勤数据！";
            mLlogService.save(new MLlog(0,"同步考勤",msg,request));//保存个日志
        }catch (Exception e){
            msg = "同步"+date+"考勤数据失败。";
            mLlogService.save(new MLlog(1,"同步考勤",msg,request));//保存个日志
        }
        return AjaxMsg.returnAjaxMsg(n,msg);
    }




    /**
     * 不推荐合并版本的，因为会报错阻止 以下的同步
     *  每天定时任务 执行同步今天的数据 重要
     *  为外部提供的Rest接口，查询本人当天的考勤记录
     */
    @RequestMapping({"/today"})
    @ResponseBody
    public AjaxMsg tbKqToday(HttpServletRequest request, HttpServletResponse response) {
        kqAccessService.writeMysqlDkInfoTodaySh();//同步上海考勤
        kqAccessService.writeMysqlDkInfoTodayTj();//同步天津考勤
        kqAccessService.writeMysqlDkInfoTodayDl();//同步大连考勤
        return AjaxMsg.returnAjaxMsg();
    }
    /**
     *  上海   分开执行防止报错
     *  每天定时任务 执行同步今天的数据 重要
     *
     */
    @RequestMapping({"/today/sh"})
    @ResponseBody
    public AjaxMsg tbKqTodaySH(HttpServletRequest request, HttpServletResponse response) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(new Date());
        int i = 0;
        String msg = "";
        try{
            i =  kqAccessService.writeMysqlDkInfoTodaySh();//同步上海考勤
            msg = "成功同步 "+i+" 条 "+ today +" 的上海考勤数据！";
            mLlogService.save(new MLlog(0,"同步考勤",msg,request));//保存个日志
        }catch (Exception e){
            msg = "同步上海"+today+"考勤数据失败。";
            mLlogService.save(new MLlog(1,"同步考勤",msg,request));//保存个日志
        }
        return AjaxMsg.returnAjaxMsg(i,msg);
    }
    /**
     * 本项目 废弃
     * 天津    分开执行防止报错
     *  每天定时任务 执行同步今天的数据 重要
     */
    @RequestMapping({"/today/tj"})
    @ResponseBody
    public AjaxMsg tbKqTodayTj(HttpServletRequest request, HttpServletResponse response) {

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(new Date());
        int i = 0;
        String msg = "";
        try{
            i =  kqAccessService.writeMysqlDkInfoTodayTj();//同步天津考勤
            msg = "成功同步 "+i+" 条 "+ today +" 的天津考勤数据！";
            mLlogService.save(new MLlog(0,"同步考勤",msg,request));//保存个日志

        }catch (Exception e){
            msg = "同步天津"+today+"考勤数据失败。";
            mLlogService.save(new MLlog(1,"同步考勤",msg,request));//保存个日志
        }
        return AjaxMsg.returnAjaxMsg(i,msg);
    }
    /**
     * 大连    分开执行防止报错
     *  每天定时任务 执行同步今天的数据 重要
     *  为外部提供的Rest接口，查询本人当天的考勤记录
     */
    @RequestMapping({"/today/dl"})
    @ResponseBody
    public AjaxMsg tbKqTodayDl(HttpServletRequest request, HttpServletResponse response) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(new Date());
        int i = 0;
        String msg = "";
        try{
            i =  kqAccessService.writeMysqlDkInfoTodayDl();//同步大连考勤
            msg = "成功同步 "+i+" 条 "+ today +" 的大连考勤数据！";
            mLlogService.save(new MLlog(0,"同步考勤",msg,request));//保存个日志
        }catch (Exception e){
            msg = "同步大连"+today+"考勤数据失败。";
            mLlogService.save(new MLlog(1,"同步考勤",msg,request));//保存个日志
        }
        return AjaxMsg.returnAjaxMsg(i,msg);
    }




    /**
     *  为外部提供的Rest接口，查询本人当天的考勤记录
     *  暂时不用  在FanWeiController中
     */
 /*   @RequestMapping({"/kq/user/today"})
    @ResponseBody
    public AjaxMsg getUserKqToday(HttpServletRequest request,HttpServletResponse response) {
        String date = request.getParameter("date");
        String userId = request.getParameter("userId");
        List<KqData> list = null;
        if(StringUtils.hasText(date) && StringUtils.hasText(userId)   ){
            list = kqDataService.getListByUserIdDate(Integer.valueOf(userId),date);
        }
        return AjaxMsg.returnAjaxMsg(list);
    }*/


    /**
     *  为外部提供的Rest接口，查询本人当天的 上午第一次 考勤记录
     *  暂时不用
     */
    @RequestMapping({"/user/today/am"})
    @ResponseBody
    public AjaxMsg getUserKqTodayAm(HttpServletRequest request, HttpServletResponse response) {
        String date = request.getParameter("date");
        String userId = request.getParameter("userId");
        KqData kqData = null;
        if(StringUtils.hasText(date) && StringUtils.hasText(userId)   ){
            kqData = kqDataService.getKqDataAmOrPm(Integer.valueOf(userId),date,"am");
        }
        return AjaxMsg.returnAjaxMsg(kqData);
    }
    //下午 最后一次
    @RequestMapping({"/user/today/pm"})
    @ResponseBody
    public AjaxMsg getUserKqTodayPm(HttpServletRequest request, HttpServletResponse response) {
        String date = request.getParameter("date");
        String userId = request.getParameter("userId");
        KqData kqData = null;
        if(StringUtils.hasText(date) && StringUtils.hasText(userId)   ){
            kqData = kqDataService.getKqDataAmOrPm(Integer.valueOf(userId),date,"pm");
        }
        return AjaxMsg.returnAjaxMsg(kqData);
    }

}
